﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace PokemonBattle.PokemonData
{
    [Serializable()]
    public class Type : DataStream
    {
        public Type()
        {
            this.TypeEffects = new Dictionary<string, double>();
            this.WeatherEffects = new Dictionary<Weather, double>();
        }

        #region props

        public Image Image { get; set;}
        public string Name{ get; set;}
        public Dictionary<string, double> TypeEffects {get; private set;}
        public Dictionary<Weather, double> WeatherEffects{get; private set;}

        #endregion

        #region abstract

        public override void Load(Stream input)
        {
            BinaryReader reader = new BinaryReader(input);
            this.Name = reader.ReadString();
            int num = reader.ReadInt32();
            for (int i = 0; i < num; i++)
            {
                this.TypeEffects.Add(reader.ReadString(), reader.ReadDouble());
            }
            num = reader.ReadInt32();
            for (int j = 0; j < num; j++)
            {
                this.WeatherEffects.Add((Weather)reader.ReadInt32(), reader.ReadDouble());
            }
            BinaryFormatter formatter = new BinaryFormatter();
            this.Image = formatter.Deserialize(input) as Image;
        }

        public override void Save(Stream output)
        {
            BinaryWriter writer = new BinaryWriter(output);
            writer.Write(this.Name);
            writer.Write(this.TypeEffects.Count);
            foreach (var item in this.TypeEffects)
            {
                writer.Write(item.Key);
                writer.Write(item.Value);
            }

            writer.Write(this.WeatherEffects.Count);
            foreach (var item in this.WeatherEffects)
            {
                writer.Write((int)item.Key);
                writer.Write(item.Value);
            }
            new BinaryFormatter().Serialize(output, this.Image);
        }

        #endregion

        public static Type FromStream(Stream input)
        {
            return FromStream<Type>(input);
        }
    }
}